<?php

namespace App\Jobs;


use App\Libraries\Easemob\Easemob;
use App\Libraries\Tools\Msg;
use App\Models\Msg\MsgSystem;
use Illuminate\Support\Facades\Log;
use JMessage\IM\Message;
use JMessage\JMessage;

class SendReceiveHongBaoMsgJob extends Job
{


    private $user_id;


    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

    /**
     * Execute the job.
     *
     * @param MsgSystem $msgSystem
     * @return bool
     * @throws \Exception
     */
    public function handle(MsgSystem $msgSystem)
    {
        //发送领取红包消息
        $msgSystem->to_id = $this->user_id;
        $msgSystem->message = Msg::receive_hong_bao();
        $msgSystem->type = 1;
        $msgSystem->save();
        $client = new JMessage(env('JMSG_APPKEY'), env('JMSG_SECRET'));
        $message = new Message($client);
        $message->sendText(1, [
            'type' => 'admin',
            'id'   => 'beboy_6',
            'name' => '系统消息'
        ], [
            'type' => 'single',
            'id'   => 'beboy_'.$this->user_id,
        ], [
            'text' => Msg::receive_hong_bao(),
            'extras' => ['user_id'=>6, 'type'=>1]
        ], [
            'notifiable' => false
        ], [
            'offline' => true
        ]);
//        $data = Easemob::singleton()->sendText(6, 'users', [$this->user_id], Msg::receive_hong_bao(), ['user_id'=>6, 'type'=>1]);
//        if(!empty($data['error'])){
//            throw new \Exception('send receive hong bao msg Fail');
//        }
        return true;
    }
}
